﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.ComponentModel;

namespace MLY.Bll
{ 
    [DataObject]
    public class clsMessage
    {

        #region Constructors
        // ==================================
        public clsMessage()
        {
            _db = new Dal.lqKbmDataContext(g.AppConnectionString);
            _DataRow = new Dal.TableMessage();
            IsExist = false;
        }

        public clsMessage(int MsgId)
        {
            _db = new Dal.lqKbmDataContext(g.AppConnectionString);
            _DataRow = (from o in _db.TableMessages
                        where (o.MsgId == MsgId)
                        select o).First();

            IsExist = true;
        }
        // ==================================
        #endregion

        #region Static Members
        // ==================================

        public static Dal.TableMessage getMsgById(int id)
        {
            using (Dal.lqKbmDataContext db = new MLY.Dal.lqKbmDataContext(g.AppConnectionString))
            {
                return (from o in db.TableMessages where o.MsgId == id select o).First();
            }
        }

        public static List<Dal.TableOrganizationRelation> GetContractors(int OrgId)
        {
            using (Dal.lqKbmDataContext db = new MLY.Dal.lqKbmDataContext(g.AppConnectionString))
            {
                return (from o in db.TableOrganizationRelations where (int)o.CustomerId == OrgId select o).ToList<Dal.TableOrganizationRelation>();
            }
        }

       

        // ==================================
        #endregion

        #region Public Methods
        // ==================================
        public static void DeleteMessageRelations(int MsgId)
        {
            Dal.lqKbmDataContext _db = new Dal.lqKbmDataContext(g.AppConnectionString);
            var deleteMessageRelations =
            from OrgMessage in _db.TableMessagesRelations
            where OrgMessage.MsgId == MsgId
            select OrgMessage;

            foreach (var OrgMessage in deleteMessageRelations)
            {
                _db.TableMessagesRelations.DeleteOnSubmit(OrgMessage);
            }

            try
            {
                _db.SubmitChanges();
            }
            catch //(Exception e)
            {
                throw new Exception();
            }
        }

        public static bool DeleteMsg(int MsgId)
        {
            Dal.lqKbmDataContext _db = new Dal.lqKbmDataContext(g.AppConnectionString);
            var deleteMessageRelations =
            (from OrgMessage in _db.TableMessagesRelations
            where OrgMessage.MsgId == MsgId
            select OrgMessage).FirstOrDefault();

            if (deleteMessageRelations != null) return false;

            else 
            {
                var deleteMessage =
                (from OrgMessage in _db.TableMessages
                where OrgMessage.MsgId == MsgId
                select OrgMessage).FirstOrDefault();
                _db.TableMessages.DeleteOnSubmit(deleteMessage);
            }

            try
            {
                _db.SubmitChanges();
                return true;
            }
            catch //(Exception e)
            {
                return false;
                throw new Exception();
            }

        }

        public void Save()
        {
            if (IsExist)
            {
                
                _DataRow.UpdateDate = DateTime.Now;
                _DataRow.UpdatedBy = clsUser.CurrentUserId;
            }
            else
            {
                _DataRow.DateCreated = DateTime.Now;
                _DataRow.UpdateDate = DateTime.Now;
                _DataRow.UpdatedBy = clsUser.CurrentUserId;

                _db.TableMessages.InsertOnSubmit(_DataRow);
                
            }
            _db.SubmitChanges();
        }
        // ==================================
        #endregion

        #region Public Properties
        // ==================================
        public Dal.TableMessage Data
        {
            get { return _DataRow; }
        }
        // ==================================
        #endregion

        #region Fields
        // ==================================
        public bool IsExist = false;
        Dal.lqKbmDataContext _db = null;
        Dal.TableMessage _DataRow = null;
        // ==================================
        #endregion
    }
}